package internal

import (
	"encoding/json"
	"github.com/nats-io/stan.go"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/db/mysql/db_main"
)

// 活动订阅
//func SubscribeActivityChannel() {
//	_, err := global.GetNats().QueueSubscribe(util.ACTIVITY_EVENT_CHANNEL, util.ACTIVITY_EVENT_CHANNEL_GROUP,
//		ActivityEventHandler,
//		stan.DurableName(util.ACTIVITY_EVENT_CHANNEL))
//	if err != nil {
//		global.Log.Errorf("订阅%s失败,err:%v", util.ACTIVITY_EVENT_CHANNEL, err)
//	}
//}

func ActivityEventHandler(msg *stan.Msg) {
	var finishedActivity *api_models.PubFinishedActivity
	err := json.Unmarshal(msg.Data, &finishedActivity)
	if err != nil {
		global.Log.Errorf("err :%v", err)
		return
	}
	global.Log.Debug("收到订阅事件:%+v", finishedActivity)
	switch finishedActivity.Category {
	case _const.RegActivityType:
		db_main.RegActivity(finishedActivity.Uid)
	}
}
